//
//  DR_SettingVC.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/10.
//

import UIKit
import SnapKit

struct DR_SettingMenuItem {
    let iconName: String
    let title: String
    let action: () -> Void
}

class DR_SettingVC: DR_BaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        setupConstraints()
    }
    
    private func setupUI(){
        view.addSubview(titleLabel)
        view.addSubview(containerView)
        
        // 添加菜单项
        for (index, item) in menuItems.enumerated() {
            let menuItemView = createMenuItemView(iconName: item.iconName, title: item.title, index: index)
            containerView.addSubview(menuItemView)
            menuItemViews.append(menuItemView)
        }
    }

    private func setupConstraints(){
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide).offset(20)
            make.height.equalTo(44)
        }
        
        containerView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.height.equalTo(menuItems.count * 60 + (menuItems.count - 1) * 1)
        }
        
        // 设置每个菜单项的约束
        for (index, menuItemView) in menuItemViews.enumerated() {
            menuItemView.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.height.equalTo(60)
                if index == 0 {
                    make.top.equalToSuperview()
                } else {
                    make.top.equalTo(menuItemViews[index - 1].snp.bottom).offset(1)
                }
            }
        }
    }
    
    private func createMenuItemView(iconName: String, title: String, index: Int) -> UIView {
        let container = UIView()
        container.backgroundColor = .clear
        
        let iconImageView = UIImageView()
        iconImageView.image = UIImage(named: iconName)
        iconImageView.contentMode = .scaleAspectFill
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = UIFont.systemFont(ofSize: 16)
        titleLabel.textColor = .black
        
        let arrowImageView = UIImageView()
        arrowImageView.image = UIImage(named: "setting_right")
        arrowImageView.contentMode = .scaleAspectFill
        
        // 添加分隔线（除了最后一个）
        let separatorView = UIView()
        separatorView.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0)
        separatorView.isHidden = (index == menuItems.count - 1)
        
        container.addSubview(iconImageView)
        container.addSubview(titleLabel)
        container.addSubview(arrowImageView)
        container.addSubview(separatorView)
        
        iconImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconImageView.snp.trailing).offset(16)
            make.centerY.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        separatorView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(56)
            make.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 添加点击手势
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(menuItemTapped(_:)))
        container.addGestureRecognizer(tapGesture)
        container.isUserInteractionEnabled = true
        
        // 使用tag存储索引
        container.tag = index
        
        return container
    }
    
    @objc private func menuItemTapped(_ gesture: UITapGestureRecognizer) {
        guard let view = gesture.view else { return }
        let index = view.tag
        if index < menuItems.count {
            menuItems[index].action()
        }
    }
    
    private func handleLanguageTap() {
        
        self.pushHidingTabBar(DR_LanguageVC(), animated: true)
    }
    
    private func handleVersionTap() {
        print("Version tapped")
        // TODO: 显示版本信息
    }
    
    private func handlePolicyTap() {
        print("Policy tapped")
        // TODO: 显示版本信息
    }

    private let titleLabel: UILabel = {
        let v = UILabel()
        v.text = "common_setting".localized
        v.textAlignment = .center
        v.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        v.textColor = UIColor.black
        return v
    }()
    
    private let containerView: UIView = {
        let view = UIView()
        view.backgroundColor = "#F6F7FF".toColor
        view.layer.cornerRadius = 16
        view.layer.masksToBounds = true
        return view
    }()
    
    private var menuItemViews: [UIView] = []
    
    private lazy var menuItems: [DR_SettingMenuItem] = [
        DR_SettingMenuItem(iconName: "setting_language", title: "setting_language".localized, action: { [weak self] in
            self?.handleLanguageTap()
        }),
        DR_SettingMenuItem(iconName: "setting_policy", title: "setting_policy".localized, action: { [weak self] in
            self?.handlePolicyTap()
        }),
        DR_SettingMenuItem(iconName: "setting_version", title: "setting_version".localized, action: { [weak self] in
            self?.handleVersionTap()
        })
    ]

}
